# -*- coding: utf-8 -*-
import tornado
import app.tools.SafeMonitorTools.api as api
import app.tools.SafeMonitorTools.m_position_layer_type as layerType
from app.views import SmBaseHandler
LayerType = layerType.LayerType # kj83里面 m_position表格中的 layer字段对应值的字典
mine_id = layerType.default_MineId
class zTreeDataHandler(SmBaseHandler):
    @tornado.gen.coroutine
    def get(self):
        print('data-handler')
        type = self.get_query_argument('type')
        responses = []
        if type == 'staff':
            rows = yield api.query_staff_data(mine_id, layer=LayerType['SMStaff'])
            print(rows)
            for row in rows:
                staff = {}
                staff['node_id'] = row[0]
                staff['status'] = row[1]
                staff['save_time'] = row[2].ctime() # 将时间对象转换为时间戳，传给客户端
                staff['count'] = row[3]
                staff['alarm'] = row[4]
                staff['placement'] = row[5]
                staff['state'] = row[6]
                responses.append(staff)
        elif type == 'broadcast':
            rows = yield api.query_broadcast_data(mine_id,layer=LayerType['SMBroadCast'])
            for row in rows:
                broad = {}
                broad['mine_id'] = row[0]
                broad['layer'] = row[1]
                broad['node_id'] = row[2]
                broad['status'] = row[3]
                broad['save_time'] = row[4].ctime()
                broad['alarm'] = row[5]
                broad['place'] = row[6]
                broad['parent_id'] = row[7]
                broad['node_type'] = row[8]
                responses.append(broad)
        elif type == 'video':
            rows = yield api.query_video_data(mine_id,layer=LayerType['SMVideo'])
            for row in rows:
                video = {}
                video['mine_id'] = row[0]
                video['node_id'] = row[1]
                video['is_parent'] = False
                video['parent_id'] = row[2]
                video['node_type'] = row[3]
                video['place'] = row[4]
                video['deviceId'] = row[5]
                video['manufacture'] = row[6]
                video['ip'] = row[7]
                video['port'] = row[8]
                video['username'] = row[9]
                video['password'] = row[10]
                video['codec'] = row[11]
                video['channel'] = row[12]
                video['url'] = row[13]
                video['create_time'] = row[14].ctime()
                video['name'] = " - ".join([video['place'], video['node_type']])
                responses.append(video)
        elif type == 'gas':
            rows = yield api.query_gas_data(mine_id,layer=LayerType['SMGas'])
            for row in rows:
                gas = {}
                gas['mine_id'] = row[0]
                gas['layer'] = row[1]
                gas['node_id'] = row[2]
                gas['status'] = row[3]
                gas['alarm'] = row[4]
                gas['real_data'] = row[5]
                gas['save_time'] = row[6].ctime()

                gas['parent_id'] = row[7]
                gas['node_type'] = row[8]
                gas['x'] = row[9]
                gas['y'] = row[10]
                gas['real_data'] = row[11]
                responses.append(gas)
        self.finish({'data': responses})
